package com.base.conn;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

public class ServerConnBaseHandler extends ChannelInboundHandlerAdapter {
	public static final ServerConnBaseHandler INSTANCE_CONN_HANDLER = new ServerConnBaseHandler();
	/**
     * 收到数据时调用
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        try {
            ServerConnBase.ReciveMessage(ctx,msg);
        } finally {
            // 抛弃收到的数据
            ReferenceCountUtil.release(msg);
        }

    }
    
    /**
     * 当Netty由于IO错误或者处理器在处理事件时抛出异常时调用
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}
